package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import com.sun.tools.javah.Gen;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.entity.TaskSub;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/6
 * Time: 17:52
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TaskMainDao extends StringDao<TaskMain> {


    public List<TaskMain> queryAboutMyTask(TaskMainFilter filter){

        GenericQuery query = createQuery("obj");
        query.append(" and del is null");
        if (StringUtils.isNotBlank(filter.getMainName())) {
            query.append(" and mainName like :mainName").setParam("mainName", StringUtils.getLikewords(filter.getMainName()));
        }

        query.append(" and (  createUser=:createUser").setParam("createUser",filter.getCreateUser());
        query.append(" or crucialUser=:crucialUser").setParam("crucialUser",filter.getCrucialUser());
        query.append(" or inspectionId=:inspectionId )").setParam("inspectionId",filter.getInspectionId());


        query.setOrder("sysId",GenericQuery.DESC);
        return query.listResult(filter);

    }

    public List<TaskMain> queryFilter(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。
        if (StringUtils.isNotBlank(filter.getSysId())) {
            query.append(" and sysId = :sysId").setParam("sysId", filter.getSysId());
        }

        if (StringUtils.isNotBlank(filter.getStatus())) {
            if("!4".equals(filter.getStatus())){
                query.append(" and  status <> '4' and status <> '10'");
            }else if("4".equals(filter.getStatus())){
                query.append("  and  status = '4' and status <> '10'");
            }else{
                query.append(" and status =:status").setParam("status", filter.getStatus());
            }
        }

        if (StringUtils.isNotBlank(filter.getCategoryId())) {
            query.append(" and categoryId = :categoryId").setParam("categoryId", filter.getCategoryId());
        }
        if (StringUtils.isNotBlank(filter.getMainName())) {
            query.append(" and mainName like :mainName").setParam("mainName", StringUtils.getLikewords(filter.getMainName()));
        }
        if (StringUtils.isNotBlank(filter.getInspectionId()) && !"ALL".equals(filter.getInspectionId())) {
            query.append(" and inspectionId = :inspectionId").setParam("inspectionId", filter.getInspectionId());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        if (StringUtils.isNotBlank(filter.getCreateUser())) {
            query.append(" and createUser = :createUser").setParam("createUser", filter.getCreateUser());
        }

        if (StringUtils.isNotBlank(filter.getAuditorId())) {
            query.append(" and auditorId =:auditorId").setParam("auditorId", filter.getAuditorId());
        }
        if (StringUtils.isNotBlank(filter.getCrucialState())) {
            query.append(" and crucialState = :crucialState").setParam("crucialState", filter.getCrucialState());
        }
       if (StringUtils.isNotBlank(filter.getTime())) {
            query.append(" and createDate like :createDate").setParam("createDate", StringUtils.getLikewords(filter.getTime()));
        }
       if(StringUtils.isNotBlank(filter.getGjId()))
       {
           query.append(" and gjId=:gjId").setParam("gjId",filter.getGjId());
       }
        if ("DUCHA".equals(filter.getOther())) {
            //如果是督查单位查看。过滤掉重大类型
            query.append(" and overdueDept is null");
        }

        if (StringUtils.isNotBlank(filter.getCrucialUser())) {
            if ("ZAIBAN".equals(filter.getCrucialUser())) {
                //攻坚在办
                query.append(" and crucial_user = create_user ");
            } else if ("ALL".equals(filter.getCrucialUser())) {
                //攻坚领导 只有一个
                query.append(" and crucial_user is not null ");
            } else {
                query.append(" and crucialUser=:crucialUser").setParam("crucialUser", filter.getCrucialUser());
            }
        } /*else {
            query.append(" and (crucial_user is null or crucial_user='') ");
        }*/

        // 督查立项 单位待立项
        if ("ALL".equals(filter.getOverdueDept())) {
            query.append(" and obj.overdueDept is not null ");
        }
        //单位 ALL 的时候不走   只当不为All  查询单位的  单位自身的所有攻坚任务
        else if (StringUtils.isNotBlank(filter.getOverdueDept())) {
            query.append(" and obj.overdueDept = :overdueDept").setParam("overdueDept", filter.getOverdueDept());
        }

        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        } else {
            Paging paging = new Paging(0);
            paging.setSortCol("sysId");
            paging.setSortVal("desc");
            filter.setPaging(paging);
        }
        List<TaskMain> list = query.listResult(filter);
        return list;
    }


    public List<TaskMain> queryByStatus(String s) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。

        query.append(" and status =:status").setParam("status", "2");
        return query.listResult();
    }


    /**
     * 获取攻坚任务
     * @param filter
     * @return
     */
    public List<TaskMain> getGjTask(TaskMainFilter filter)
    {
        StringBuffer hql =new StringBuffer();
//        hql.append("SELECT DISTINCT gjId,sysId,mainName,gJFileName,gJFile,crucialUser,inspectionId from  TaskMain  where imp ='2'  ");
        hql.append(" from TaskMain where imp =2 group by gjId  order by sysId desc ");
        GenericQuery query = create(hql.toString());

        //query.setOrder("sysId","desc");


        return query.listResult(filter);


    }

    /**
     * 未完成任务
     *
     * @param filter
     * @return
     */
    public List<TaskMain> unDoneFilter(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。
        if (StringUtils.isNotBlank(filter.getSysId())) {
            query.append(" and sysId = :sysId").setParam("sysId", filter.getSysId());
        }
        if (StringUtils.isNotBlank(filter.getCategoryId())) {
            query.append(" and categoryId = :categoryId").setParam("categoryId", filter.getCategoryId());
        }
        if (StringUtils.isNotBlank(filter.getMainName())) {
            query.append(" and mainName like :mainName").setParam("mainName", StringUtils.getLikewords(filter.getMainName()));
        }
        if (StringUtils.isNotBlank(filter.getInspectionId())) {
            query.append(" and inspectionId = :inspectionId").setParam("inspectionId", filter.getInspectionId());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        if (StringUtils.isNotBlank(filter.getCreateUser())) {
            query.append(" and createUser = :createUser").setParam("createUser", filter.getCreateUser());
        }
        if (StringUtils.isNotBlank(filter.getAuditorId())) {
            query.append(" and auditorId =:auditorId").setParam("auditorId", filter.getAuditorId());
        }
        if (StringUtils.isNotBlank(filter.getCrucialUser())) {
            if ("ZAIBAN".equals(filter.getCrucialUser())) {
                //攻坚在办
                query.append(" and crucial_user = create_user ");
            } else if ("ALL".equals(filter.getCrucialUser())) {
                //攻坚领导 只有一个
                query.append(" and crucial_user is not null ");
            } else {
                query.append(" and crucialUser=:crucialUser").setParam("crucialUser", filter.getCrucialUser());
            }
        }
        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        }else {
            Paging paging = new Paging(0);
            paging.setSortCol("sysId");
            paging.setSortVal("desc");
            filter.setPaging(paging);
        }
        List<TaskMain> list = query.listResult(filter);
        return list;
    }

    /**
     * 待办单位重大事项
     *
     * @param filter
     * @return
     */
    public List<TaskMain> queryDBOverdueDept(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。

        query.append(" and overdueDept = :overdueDept").setParam("overdueDept", filter.getOverdueDept());
        if (StringUtils.isNotBlank(filter.getMainName())){
            query.append(" and mainName = :mainName").setParam("mainName", filter.getMainName());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        query.append(" and overdueDeptState= :overdueDeptState").setParam("overdueDeptState", "0");
        return query.listResult(filter);
    }

    public List<TaskMain> queryZBOverdueDept(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。
        if (StringUtils.isNotBlank(filter.getMainName())){
            query.append(" and mainName = :mainName").setParam("mainName", filter.getMainName());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        query.append(" and overdueDept = :overdueDept").setParam("overdueDept", filter.getOverdueDept());
        query.append(" and status != :status").setParam("status", "4");
        query.setOrder("sysId",GenericQuery.DESC);
        return query.listResult(filter);
    }

    public List<TaskMain> queryWCOverdueDept(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。
        if (StringUtils.isNotBlank(filter.getMainName())){
            query.append(" and mainName = :mainName").setParam("mainName", filter.getMainName());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        query.append(" and overdueDept = :overdueDept").setParam("overdueDept", filter.getOverdueDept());
        query.append(" and status = :status").setParam("status", "4");
        return query.listResult(filter);
    }

    public List<TaskMain> queryOverdueDept(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。
        if (StringUtils.isNotBlank(filter.getMainName())){
            query.append(" and mainName = :mainName").setParam("mainName", filter.getMainName());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        query.append(" and overdueDept = :overdueDept").setParam("overdueDept", filter.getOverdueDept());
        query.setOrder("sysId",GenericQuery.DESC);
        return query.listResult(filter);
    }

    /**
     * 查询删除过的任务(回收站)
     *
     * @param filter
     * @return
     */
    public List<TaskMain> queryDelTask(TaskMainFilter filter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del = :del").setParam("del", "0");//只查询删除标识为0的记录。
        if (StringUtils.isNotBlank(filter.getSysId())) {
            query.append(" and sysId = :sysId").setParam("sysId", filter.getSysId());
        }
        if (StringUtils.isNotBlank(filter.getCategoryId())) {
            query.append(" and categoryId = :categoryId").setParam("categoryId", filter.getCategoryId());
        }
        if (StringUtils.isNotBlank(filter.getMainName())) {
            query.append(" and mainName like :mainName").setParam("mainName", StringUtils.getLikewords(filter.getMainName()));
        }
        if (StringUtils.isNotBlank(filter.getInspectionId()) && !"ALL".equals(filter.getInspectionId())) {
            query.append(" and inspectionId = :inspectionId").setParam("inspectionId", filter.getInspectionId());
        }
        if (StringUtils.isNotBlank(filter.getStartTime())) {
            query.append(" and timeLimit >= :startTime").setParam("startTime", filter.getStartTime());
        }
        if (StringUtils.isNotBlank(filter.getEndTime())) {
            query.append(" and timeLimit <= :endTime").setParam("endTime", filter.getEndTime());
        }
        if (StringUtils.isNotBlank(filter.getCreateUser())) {
            query.append(" and createUser = :createUser").setParam("createUser", filter.getCreateUser());
        }
        if (StringUtils.isNotBlank(filter.getStatus())) {
            query.append(" and status =:status").setParam("status", filter.getStatus());
        }
        if (StringUtils.isNotBlank(filter.getAuditorId())) {
            query.append(" and auditorId =:auditorId").setParam("auditorId", filter.getAuditorId());
        }
        if (StringUtils.isNotBlank(filter.getCrucialState())) {
            query.append(" and crucialState = :crucialState").setParam("crucialState", filter.getCrucialState());
        }
        if (StringUtils.isNotBlank(filter.getCrucialUser())) {
            query.append(" and crucialUser=:crucialUser").setParam("crucialUser", filter.getCrucialUser());

        } /*else {
            query.append(" and (crucial_user is null or crucial_user='') ");
        }*/

        // 督查立项 单位待立项
        if ("ALL".equals(filter.getOverdueDept())) {
            query.append(" and obj.overdueDept is not null ");
        }
        //单位 ALL 的时候不走   只当不为All  查询单位的  单位自身的所有攻坚任务
        else if (StringUtils.isNotBlank(filter.getOverdueDept())) {
            query.append(" and obj.overdueDept = :overdueDept").setParam("overdueDept", filter.getOverdueDept());
        }

        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        }else {
            Paging paging = new Paging(0);
            paging.setSortCol("sysId");
            paging.setSortVal("desc");
            filter.setPaging(paging);
        }
        List<TaskMain> list = query.listResult(filter);
        return list;

    }





}
